﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EquipMentStrengtheningManager : MonoBehaviour
{
    public static EquipMentStrengtheningManager ESM;
    Text moneyValue;//元宝数
    public int clickSix = 2;//默认点击的项链
    public bool upStrengthening = true;
    public int id;//唯一id
    public GameObject equipmentShowImage;//装备图片
    public GameObject attributeImage;//详细强化内容
    public Text name1;
    public Text attribute1;
    public Text grade1;
    public Text name2;
    public Text attribute2;
    public Text grade2;
    public Button btn1;//一键强化按钮
    public Button btn2;//强化按钮
    public Text needMoney;//强化需要的元宝数
    public MainItem mainItem;
    public GameObject parentObject;//背包格子控件
    public ShowEquipment showEquipmentPrefab;//预制体
    public Image[] toggles;
    public GameObject Panel;//toggle的父对象
    public GameObject qianghua2;//强化成功特效
    public GameObject yindao10;
    public GameObject yindao11;
    private void Awake()
    {
        ESM = this;
    }
    void Start()
    {
        
        moneyValue = GameObject.Find("moneyValue").GetComponent<Text>();
        moneyValue.text = VariableDocument.GetInstance.money.ToString();
        B();
        for(int j = 0; j < 6; j++)
        {
            if (VariableDocument.GetInstance.equiped[j] != -1)
            {
                toggles[j].gameObject.SetActive(true);
                toggles[j].gameObject.transform.GetChild(0).GetComponent<Image>().sprite = mainItem.itemList[VariableDocument.GetInstance.equiped[j]].itemImage;
            }
            else
            {
                toggles[j].gameObject.SetActive(false);
            }
        }
        yindao10.transform.GetChild(0).gameObject.SetActive(false);
        yindao10.transform.GetChild(1).gameObject.SetActive(false);
        yindao11.transform.GetChild(0).gameObject.SetActive(false);
        yindao11.transform.GetChild(1).gameObject.SetActive(false);
        if (VariableDocument.GetInstance.yindaoid == 9 || VariableDocument.GetInstance.yindaoid == 18)
        {
            yindao10.transform.GetChild(0).gameObject.SetActive(true);
            yindao10.transform.GetChild(1).gameObject.SetActive(true);
        }
        MMM();
        NNN();
        Panel.transform.GetChild(clickSix).GetChild(0).GetChild(1).gameObject.SetActive(true);
    }

    void Update()
    {
        moneyValue.text = VariableDocument.GetInstance.money.ToString();
        A();
	}
    //已穿戴装备的升级
    //单选模式
    //展示有需要的元宝数、升级前的属性值，升级后的属性值，装备的图片
    //如果还没有装备，强化和一键强化按钮不能用，并且不展示图片
    private void A()
    {
        //代表上方强化
        if(upStrengthening)
        {
            if (VariableDocument.GetInstance.equiped[clickSix] != -1)//不等于-1代表着已装备
            {
                //Debug.Log("aaa");
                id = VariableDocument.GetInstance.equiped[clickSix];
                equipmentShowImage.GetComponent<Image>().color = new Color(1, 1, 1, 1);
                equipmentShowImage.GetComponent<Image>().sprite = mainItem.itemList[id].itemImage;
                name1.text = mainItem.itemList[id].itemName;
                name2.text = mainItem.itemList[id].itemName;
                attribute1.text = VariableDocument.GetInstance.equipmentAddText[clickSix, 0] + ":" +
                    mainItem.itemList[id].attribute[VariableDocument.GetInstance.equipmentAddIndex[clickSix, 0]] + "\n" +
                    VariableDocument.GetInstance.equipmentAddText[clickSix, 1] + ":" +
                    mainItem.itemList[id].attribute[VariableDocument.GetInstance.equipmentAddIndex[clickSix, 1]];
                attribute2.text = VariableDocument.GetInstance.equipmentAddText[clickSix, 0] + ":" +
                    (mainItem.itemList[id].attribute[VariableDocument.GetInstance.equipmentAddIndex[clickSix, 0]] +
                    VariableDocument.GetInstance.upgrade_add_attribute[VariableDocument.GetInstance.equipmentAddIndex[clickSix, 0]]) + "\n" +
                    VariableDocument.GetInstance.equipmentAddText[clickSix, 1] + ":" +
                    (mainItem.itemList[id].attribute[VariableDocument.GetInstance.equipmentAddIndex[clickSix, 1]] +
                    VariableDocument.GetInstance.upgrade_add_attribute[VariableDocument.GetInstance.equipmentAddIndex[clickSix, 1]]);
                grade1.text = "+" + mainItem.itemList[id].itemGrade;
                grade2.text = "+" + (mainItem.itemList[id].itemGrade + 1);
                needMoney.text = "需要:         " + ((mainItem.itemList[id].itemGrade + 1) * 200);
                btn1.enabled = true;
                btn2.enabled = true;
            }
            else
            {
                equipmentShowImage.GetComponent<Image>().color = new Color(1, 1, 1, 0);
                name1.text = "";
                name2.text = "";
                attribute1.text = "";
                attribute2.text = "";
                grade1.text = "";
                grade2.text = "";
                needMoney.text = needMoney.text = "需要:         000";
                btn1.enabled = false;
                btn2.enabled = false;
            }
        }
    }
    //上层
    public void ClickIndex(int a)
    {
        //Debug.Log("=====" + Panel.transform.childCount);
        NNN();
        MMM();
        Panel.transform.GetChild(a).GetChild(0).GetChild(1).gameObject.SetActive(true);
        upStrengthening = true;
        clickSix = a;
    }
    //所有toggle框特效消失
    public void NNN()
    {
        //Debug.Log("====="+ Panel.transform.childCount);
        for (int fg = 0; fg < Panel.transform.childCount; fg++)
        {
            Panel.transform.GetChild(fg).GetChild(0).GetChild(1).gameObject.SetActive(false);
        }
    }
    //所有装备框特效消失
    public void MMM()
    {
        for (int fg = 0; fg < parentObject.transform.childCount; fg++)
        {
            parentObject.transform.GetChild(fg).GetChild(2).gameObject.SetActive(false);
        }
    }
    //点击一键强化
    public void ClickBtn1()
    {
        while(VariableDocument.GetInstance.money >= (mainItem.itemList[id].itemGrade + 1) * 200)
        {
            VariableDocument.GetInstance.money -= ((mainItem.itemList[id].itemGrade + 1) * 200);
            mainItem.itemList[id].itemGrade += 1;
            clickSix = mainItem.itemList[id].itemCategory;
            mainItem.itemList[id].attribute[VariableDocument.GetInstance.equipmentAddIndex[clickSix, 0]] += VariableDocument.GetInstance.upgrade_add_attribute[VariableDocument.GetInstance.equipmentAddIndex[clickSix, 0]];
            mainItem.itemList[id].attribute[VariableDocument.GetInstance.equipmentAddIndex[clickSix, 1]] += VariableDocument.GetInstance.upgrade_add_attribute[VariableDocument.GetInstance.equipmentAddIndex[clickSix, 1]];
        }
        if (VariableDocument.GetInstance.money > 200)
        {
            qianghua2.SetActive(true);
            Invoke("qianghuaxiaoshi", 1f);
        }
        if (!upStrengthening)
		{
			B();
            
            ShowEquipment.SE.AfterClick2();
		}
        parentObject.transform.GetChild(VariableDocument.GetInstance.indexInParent).GetChild(2).gameObject.SetActive(true);

    }
    void qianghuaxiaoshi()
    {
        qianghua2.SetActive(false);
    }
    //点击强化
    public void ClickBtn2()
    {
        Debug.Log("===强化按钮===");
        if (VariableDocument.GetInstance.yindaoid == 9 || VariableDocument.GetInstance.yindaoid == 18)
        {
            yindao10.transform.GetChild(0).gameObject.SetActive(false);
            yindao10.transform.GetChild(1).gameObject.SetActive(false);
            yindao11.transform.GetChild(0).gameObject.SetActive(true);
            yindao11.transform.GetChild(1).gameObject.SetActive(true);
            VariableDocument.GetInstance.yindaoid += 3;
        }
        //判断元宝数是不是足够
        if (VariableDocument.GetInstance.money >= (mainItem.itemList[id].itemGrade + 1) * 200)
        {

            VariableDocument.GetInstance.money -= ((mainItem.itemList[id].itemGrade + 1) * 200);
            mainItem.itemList[id].itemGrade += 1;
            clickSix = mainItem.itemList[id].itemCategory;
            mainItem.itemList[id].attribute[VariableDocument.GetInstance.equipmentAddIndex[clickSix, 0]] += VariableDocument.GetInstance.upgrade_add_attribute[VariableDocument.GetInstance.equipmentAddIndex[clickSix, 0]];
            mainItem.itemList[id].attribute[VariableDocument.GetInstance.equipmentAddIndex[clickSix, 1]] += VariableDocument.GetInstance.upgrade_add_attribute[VariableDocument.GetInstance.equipmentAddIndex[clickSix, 1]];
            qianghua2.SetActive(true);
            Invoke("qianghuaxiaoshi", 1f);
        }
		if (!upStrengthening)
		{
			B();
            ShowEquipment.SE.AfterClick2();
            
            //Debug.Log(VariableDocument.GetInstance.indexInParent);
        }
        //Debug.Log("-----"+VariableDocument.GetInstance.indexInParent);
        //Debug.Log("name"+parentObject.transform.GetChild(VariableDocument.GetInstance.indexInParent).GetChild(2).gameObject.name);
        //parentObject.transform.GetChild(VariableDocument.GetInstance.indexInParent).GetChild(2).gameObject.SetActive(true);
    }


    //装备背包直接升级
    //装备背包显示
    int ttt = 0;
    public void B()
    {      
        ttt = 0;
        //销毁弹框背包格子下的子物体
        for (int i = 0; i < parentObject.transform.childCount; i++)
        {
            Destroy(parentObject.transform.GetChild(i).gameObject);
        }
        //遍历mainItem
        for (int a = 0; a < mainItem.itemList.Count; a++)
        {
            if (!mainItem.itemList[a].selled)
            {
                bool canNew = true;
                for (int b = 0; b < VariableDocument.GetInstance.equiped.Length; b++)
                {
                    if (VariableDocument.GetInstance.equiped[b] == a)
                    {
                        canNew = false;
                        break;
                    }
                }
                if (canNew)
                {
                    //加载预制体
                    ShowEquipment newEquipMent = Instantiate(showEquipmentPrefab);
                    //指定父物体
                    newEquipMent.transform.SetParent(parentObject.transform);
                    //指定大小
                    newEquipMent.transform.localScale = new Vector3(1f, 1f, 1f);
                    //展示的装备的图片
                    newEquipMent.showImage.sprite = mainItem.itemList[a].itemImage;
                    //装备的等级
                    newEquipMent.itemGrade.text = "+" + mainItem.itemList[a].itemGrade;
                    //装备的id号
                    newEquipMent.id = mainItem.itemList[a].id;
                    newEquipMent.name = ttt.ToString();
                    if (ttt == VariableDocument.GetInstance.indexInParent)
                        newEquipMent.GetComponent<ShowEquipment>().ShowLight();
                    ttt++;
                }

            }

        }
        Debug.Log("b方法");
        //parentObject.transform.GetChild(id).GetChild(2).gameObject.SetActive(true);
    }
}
